package 优先算法.递归_搜索与回溯.初学递归.Pow_x_n;

/**
 *https://leetcode.cn/problems/powx-n/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        double x = 2.00000, n = -2;
        double rel = so.myPow(x, (int) n);
        System.out.println(rel);
    }
}
class Solution {
    public double myPow(double x, long n) {
        int count = 0;
        if(n < 0 && count == 0){
            n = -n;
            count++;
        }
        if(n == 0){
            return 1;
        }
        double ret = myPow(x, n / 2);
        double rel1 =ret*ret;
        double rel2 =ret*ret*x;
        if(count > 0){
            rel1 = 1/rel1;
            rel2 = 1/rel2;
        }
        return n%2 == 0? rel1:rel2;
    }
}